package com.deyuanyun.pic.ctrl.service.pushmsg;

import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspOclpointExcel;
import com.deyuanyun.pic.ctrl.task.OclPointNoticeTask;
import com.deyuanyun.pic.settings.controller.dto.BizMsgThresholdVO;
import com.deyuanyun.pic.settings.enumeration.ThresholdType;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.support.utils.BaseCodeUtils;
import com.deyuanyun.pic.settings.support.utils.CronUtils;
import com.deyuanyun.pic.settings.task.QuartzConsts;
import com.deyuanyun.pic.settings.task.SchedulerJob;
import com.deyuanyun.pic.settings.task.util.QuartzManagerUtil;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  外涂层漏损点信息录入创建任务调度
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-10-25
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class ExinspOclpointAddNotice extends AbstractPushMsgTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExinspOclpointAddNotice.class);

    @Override
    public void doPushMsgData() {
        if(super.interfaceParam.isEmpty()) {
            LOGGER.info("没有传入参数");
            return;
        }
        BizMsgThresholdVO msgThresholdVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.INTERNAL_PROTECTING_COATING_ADVISE_INPUT.getValue());

        Object objThre = msgThresholdVO.getThresholdContentJson();
        if(objThre == null || objThre == "" || !(objThre instanceof Map)) {
            LOGGER.info("没有设置对应的字段值或者不是map结构");
            return;
        }

        Map thresholdMap = (Map) objThre;
        Object dayStr = thresholdMap.get("day"); //三级
        if(dayStr == null) {
            LOGGER.info("没有找到设置项");
            return;
        }
        int day = Integer.valueOf(dayStr.toString());

        Object obj = super.interfaceParam.get(CtrlPushMsgCons.LIST_KEY);

        if(obj != null && (obj instanceof List)) {
            List<PicCtrlExinspOclpointExcel> list = (List) obj;
            List<PicCtrlExinspOclpointExcel> ljwxList = new ArrayList<PicCtrlExinspOclpointExcel>();
            for (int i = 0; i < list.size(); i++) {
                PicCtrlExinspOclpointExcel excel = list.get(i);
                String minorCode = BaseCodeUtils.getMinorCode(excel.getRepairSuggestion());
                if("REPAIR_SUGGESTED_LJWX".equals(minorCode) || "REPAIR_SUGGESTED_JHWX".equals(minorCode)) { //立即维修和建议维修都取出来
                    ljwxList.add(excel);
                }
            }
            Collections.sort(ljwxList, new Comparator<PicCtrlExinspOclpointExcel>() { //按照最迟维修排序取时间最早的一条
                @Override
                public int compare(PicCtrlExinspOclpointExcel o1, PicCtrlExinspOclpointExcel o2) {
                    return o1.getLatestRepairTime().compareTo(o2.getLatestRepairTime());
                }
            });

            if(!ljwxList.isEmpty()) {
                PicCtrlExinspOclpointExcel excel = ljwxList.get(0);
                String latestRepairTime = excel.getLatestRepairTime();
                LocalDate localDate = LocalDate.parse(latestRepairTime, DateTimeFormat.forPattern("yyyyMMdd"));
                LocalDate newLocalDate = localDate.plusDays(-day);
                Date date = CronUtils.getDefectNoticeCron(newLocalDate.toDate());
                String name = excel.getPipeLineId() + excel.getCustomNumber();
                Map dataMap = new HashMap();
                dataMap.put(OclPointNoticeTask.DAY_KEY, day);
                dataMap.put(OclPointNoticeTask.PIPE_LINE_ID_KEY, excel.getPipeLineId());
                dataMap.put(OclPointNoticeTask.CUSTOM_NUMBER, excel.getCustomNumber());
                dataMap.put(OclPointNoticeTask.LOCATION_DESCRIPTION, excel.getLocationDescription());
                dataMap.put(OclPointNoticeTask.DATE_KEY, localDate);
                //创建任务
                SchedulerJob schedulerJob = new SchedulerJob(name, QuartzConsts.OUTSIDE_ULLAGE_NOTICE_GROUP_NAME, date, OclPointNoticeTask.class, dataMap);
                QuartzManagerUtil.addJobStartByDate(schedulerJob);
            }
        }
    }

    @Override
    public void initializeBizMsgSends(Object... args) {

    }

    @Override
    public boolean valiIsPush(Map<String, Object> valiMap) {
        return false;
    }
}
